package com.mazaiting.instructions.comparisons;

import com.mazaiting.instructions.base.NoOperandsInstruction;
import com.mazaiting.rtda.MtFrame;
import com.mazaiting.rtda.MtOperandStack;

/**
 * 比较指令
 *
 * @author mazaiting
 * @since 2021/8/15
 */
public class LCmpInstruction {

    /**
     * 用于比较long变量
     * 把栈顶的两个long变量弹出，进行比较，然后把比较结果（int型0、1或-1）推入栈顶
     */
    public static class LCmp extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            long v1 = stack.popLong();
            long v2 = stack.popLong();
            if (v2 > v1) {
                stack.pushInt(1);
            } else if (v1 == v2) {
                stack.pushInt(0);
            } else {
                stack.pushInt(-1);
            }

        }
    }


}
